<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>

<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.Iterator" %>
<%@page import="com.easyattendance.services.AddStudentService"%>	
<%@page import="java.text.DateFormat" %>
<%@page import="java.text.SimpleDateFormat" %>
<%@page import="com.easyattendance.model.Student"%>
<%@page import="com.easyattendance.model.Subject"%>
<%@page import="com.easyattendance.model.StudentAttendance"%>
<%@page import="java.util.Date"%>
<%@page import="com.easyattendance.model.Student"%>
<%@ page import ="com.easyattendance.services.AddTeacherService" %>
<%@ page import ="com.easyattendance.services.AddClassService" %>
<%@ page import ="com.easyattendance.services.AddSubjectService" %>
<%@ page import ="com.easyattendance.model.Teacher" %>
<%@ page import ="com.easyattendance.model.Class" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Select Student - Elective List</title>
<!-- <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script> -->
<jsp:include page="../includeFiles.jsp" />

<!-- fooTable Links -->
<link href="../css/footable.core.css" rel="stylesheet" type="text/css" />
<link href="../css/footable.metro.css" rel="stylesheet" type="text/css" />
<script src="<%=request.getContextPath()%>/public/js/footable.js" type="text/javascript"></script>
<script type="text/javascript">
	$(function () {
		$('.footable').footable();
	});
</script>
<script src="<%=request.getContextPath()%>/public/js/footable.sort.js" type="text/javascript"></script>
<!-- ***************************************** -->
</head>

<body class="left-sidebar" onload="getValues()">

<!-- Session check : user logged in or not/ press back button -->
<jsp:include page="../common/checkSession.jsp" />
<jsp:include page="../common/checkTeacherRole.jsp" />

<% String currentUser = (String) session.getAttribute("currentUser"); 
	AddClassService addClassService = new AddClassService();
	Class tempClass = addClassService.getClassById(addClassService.getClassIdByTeacherId(currentUser));
	AddSubjectService addSubjectService = new AddSubjectService();
	AddStudentService addStudentService = new AddStudentService();
	List electiveTypes = addSubjectService.listElectiveTypesByClassId(tempClass.getClass_id());
	List subjectsInElective = null;
%>
	<!-- Wrapper -->
	<div id="wrapper">

		<!-- Content -->
		<div id="content">
			<div id="content-inner">
				
				<!-- Login -->
				
	 <form name="selectStudentElective" action="${pageContext.request.contextPath}/AddStudentElectivesServlet" method="post" class="gridview">
		
		<div>
			<label>Elective Type:</label>
			<select tabindex="1" name="elective_select" id="elective_select" value=<script>getCookie("elective_select")</script>>
				<option value="">Choose Elective Type
				<% Iterator iterator = electiveTypes.iterator();
				while(iterator.hasNext()) {
					String elective = (String)iterator.next();%>
					<option value=<%=elective%>><%=elective%>
				<% } %>
			</select>
		</div>
		<table name="studentElectiveList" id="tbl_studentElectiveList" class="footable metro-attendance">
                <% String elective = request.getParameter("elective"); %>
				<% if(elective != null) {%>
					<% if(!elective.equals("None")){ %>
						<thead>
		                    <th data-toggle="true" data-sort-initial="true">
		                        Student Roll No.
		                    </th>
		                    <th data-toggle="true">
		                        Student Name
		                    </th>
		                   <!-- <th data-hide="phone,tablet" data-sort-ignore="true">
		                        Last Name
		                    </th>  --> 
		                </thead>
		                <tbody>
		               <% List<Student> students = addStudentService.getStudentList(tempClass.getSection().getSection_id(), tempClass.getClass_id()); %>
						<tr>
						<% Iterator iterator1 = students.iterator();
							while(iterator1.hasNext()) {
								Student tempStudent = (Student)iterator1.next();%>
								<td data-value="<%=tempStudent.getStud_roll_no()%>">
									<label><%=tempStudent.getStud_roll_no()%></label>
								</td>
								
								<td data-value="<%=tempStudent.getStud_fname()%>">
									<label><%=tempStudent.getStud_fname()%> <%=tempStudent.getStud_lname()%> </label>
								</td>
								
								<% /* String elective = request.getParameter("elective"); */
			                    	subjectsInElective = addSubjectService.listSubjectsByElectiveType(elective, tempClass.getClass_id());
			                    	if(subjectsInElective != null) {
			                    		Iterator itr = subjectsInElective.iterator();
										while(itr.hasNext()) {
										Subject subject = (Subject)itr.next();%>
										<td><input type="radio" name="subject_"<%=tempStudent.getStud_roll_no()%> id="subject_"<%=tempStudent.getStud_roll_no()%> value=<%=subject.getSubject_id()%>><%=subject.getSubject_name()%></td>
										<% }
									}%>
								<%}%>
							</tr>
	                 		<% } else {%>
		                 	<tr>
		                 		<label> This Standard has no elective</label>
		                 	</tr> 
	                 	<% } %>
	                 <% } %>
				</tbody>
			</table>
			<div>	
				<input type="submit" value="Submit" class="button add_button">
			</div>
		</form>
	</div>
  </div>
  
		<!-- Sidebar -->
		<div id="sidebar">

			<!-- Sidebar Header - Logo & Name -->
			<jsp:include page="../common/sidebarHeader.jsp" />

			<jsp:include page="teacherMenu.jsp" />

			<!-- Sidebar Footer Content -->
			<jsp:include page="../common/sidebarFooter.jsp" />

		</div>

	</div>	
	<script type="text/javascript">
		$(function(){
			$("#selectStudentElectives").addClass("current_page_item");
		});
		var contextPath='<%=request.getContextPath()%>';
	</script>
	
	<!--  For drop down values -->
	<script type="text/javascript">
	
	$(function () {
	    var temp = 0;
	    $('#elective_select').click(function () {        
	    	temp++;
	        if (temp == 2) {
	            $(this).change();
	            temp = 0;
	        }         
	    }).change (function () {
	    	var elective = $("#elective_select").val();
			setCookie("elective_select",elective);
			window.location.replace(contextPath + "/views/teacher/selectStudentElectives.jsp?elective=" + elective);
	    }); 
	});
		 
		 function getValues(){
				$("#elective_select").val(getCookie("elective_select"));
			}
			
			function getCookie(name) { 
				var re = new RegExp(name + "=([^;]+)"); 
				var value = re.exec(document.cookie);
				return (value != null) ? unescape(value[1]) : null; 
			}
			
			function setCookie(name, value) {
				  var today = new Date(); 
				  var expiry = new Date(today.getTime() + 1 * 24 * 3600 * 1000); // plus 30 days
				  document.cookie=name + "=" + escape(value) + "; path=/; expires=" + expiry.toGMTString();
			 }
			
			function clearCookie(){
				deleteCookie("elective_select");
				alert("Cookies Deleted");
			}
			
			function deleteCookie(name)
			{
				var expired = new Date(today.getTime() - 24 * 3600 * 1000); // less 24 hours
				document.cookie=name + "=null; path=/; expires=" + expired.toGMTString();
			}
			
	</script>
 </body>
</html>